﻿Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Collections.Generic
Imports System.Text
Imports System.Security.Cryptography


Imports System.IO
Imports System.Linq
'Imports System.Data.OracleClient
Imports Oracle.DataAccess.Client
Imports ContAlch.Conexion


'Imports System.Collections.Generic
'Imports System.Text
'Imports System.Security.Cryptography



Public Class frmDigitalizacion

    Dim conn As New OracleConnection()
    Dim oradb As String = "DATA SOURCE=pruebas.WORLD;PERSIST SECURITY INFO=True;USER ID=OPS$SIDECOM;PASSWORD=admin"


    Public itm As Elementos
    Public ContadorBase As Integer
    Dim Viewer As AdvAlchemy.Viewer
    Dim Viewer1 As AdvAlchemy.Viewer
    Dim Viewer2 As AdvAlchemy.Viewer
    Dim Viewer3 As AdvAlchemy.Viewer
    Dim Viewer4 As AdvAlchemy.Viewer
    Dim Viewer5 As AdvAlchemy.Viewer
    Dim Viewer6 As AdvAlchemy.Viewer
    Dim Viewer7 As AdvAlchemy.Viewer
    Dim Viewer8 As AdvAlchemy.Viewer

    Dim NomBaseDatos As String
    Private AuDbOri As AdvAlchemy.Database
    Private Database As AdvAlchemy.Database
    Private Item As AdvAlchemy.Item
    Private Item2 As AdvAlchemy.Item
    Private Item3 As AdvAlchemy.Item
    Private Item4 As AdvAlchemy.Item
    Private Item5 As AdvAlchemy.Item
    Private Item6 As AdvAlchemy.Item
    Private Item7 As AdvAlchemy.Item
    Private Item8 As AdvAlchemy.Item
    Private Item9 As AdvAlchemy.Item
    Private AuBusqueda As AdvAlchemy.Query
    Public WithEvents AuApp As New AdvAlchemy.Application
    Private WithEvents AuTreeView As AdvAlchemy.TreeView

    Dim Contador
    Dim CantDoc
    Public flagPause As Boolean

    Private Sub frmDigitalizacion_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Item = Nothing
        Item2 = Nothing
        Item3 = Nothing
        Item4 = Nothing
        Item5 = Nothing
        Item6 = Nothing
        Item7 = Nothing
        Item8 = Nothing
        Item9 = Nothing

        Viewer = Nothing
        Viewer1 = Nothing
        Viewer2 = Nothing
        Viewer3 = Nothing
        Viewer4 = Nothing
        Viewer5 = Nothing
        Viewer6 = Nothing
        Viewer7 = Nothing
        Viewer8 = Nothing

        AuDbOri = Nothing
        AuApp = Nothing
        DtsReporteScan.Configuracion.WriteXml(Windows.Forms.Application.StartupPath & "\configuracion.xml")
        End
    End Sub

    Private Sub frmDigitalizacion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim strStreamW As Stream
        '  MsgBox(Application.StartupPath)
        If Not File.Exists(Windows.Forms.Application.StartupPath & "\configuracion.xml") Then
            Dim strStreamWriter As StreamWriter
            strStreamW = File.Create(Windows.Forms.Application.StartupPath & "\configuracion.xml") ' lo creamos
            strStreamWriter = New StreamWriter(strStreamW, System.Text.Encoding.Default) ' tipo de codificacion para escritura
            'escribimos en el archivo
            strStreamWriter.WriteLine("<?xml version=""1.0"" standalone=""yes""?>")
            strStreamWriter.WriteLine("<dtsReporteScan xmlns=""http://tempuri.org/dtsReporteScan.xsd"">")
            strStreamWriter.WriteLine("<Configuracion>")
            strStreamWriter.WriteLine("<Drive>c:</Drive>")
            strStreamWriter.WriteLine("<Ruta>\tmp</Ruta>")
            strStreamWriter.WriteLine("</Configuracion>")
            strStreamWriter.WriteLine("</dtsReporteScan>")
            strStreamWriter.Flush()
            strStreamWriter.Close() ' cerramos
        End If
        DtsReporteScan.Configuracion.ReadXml(Windows.Forms.Application.StartupPath & "\configuracion.xml")
        strDrive = DtsReporteScan.Configuracion.Rows(0).Item(0).ToString
        strRuta = DtsReporteScan.Configuracion.Rows(0).Item(1).ToString
        ContCampos = 0

        If Directory.Exists(strDrive.Trim & strRuta.Trim) = False Then ' si no existe la carpeta se crea
            Directory.CreateDirectory(strDrive.Trim & strRuta.Trim)
        End If



        AuApp = New AdvAlchemy.AdvApplication
        AuApp.CanWrite = True
        'AuApp.CanShare = True
        AuApp.SetOwner() ' Aqui agregar la forma

        ' pdf1.SetLicenseNumber("SERIALKEY")

        Try
            DtsReporteScan.BaseDatos.ReadXml(strDrive.Trim & strRuta.Trim & "\bases.xml")
            DtsReporteScan.Escaneo.ReadXml(strDrive.Trim & strRuta.Trim & "\data.xml")
        Catch ex As Exception
            MessageBox.Show("Error al cargar los datos favor verifique :)")
        End Try

        Dim PathArchivo = strDrive.Trim & strRuta.Trim & "\conteo.txt"
        If File.Exists(PathArchivo) Then
            MessageBox.Show("Proceso Anterior no concluido" & vbCrLf & "Se procedera a continuar con la contabilización anterior")

            Call cmdINICIAR_Click_1(sender, e)
        End If

        Dim valores() As Integer = CType([Enum].GetValues(GetType(CalcularValoreHashCs.Algorithm)), Integer())
        Dim valorDat() As String = CType([Enum].GetNames(GetType(CalcularValoreHashCs.Algorithm)), String())

        For Each val As String In valorDat
            cboAlgoritmo.Items.Add(val)
        Next

        'carga_combo_usuarios

        Dim tabla As DataTable
        tabla = New DataTable("usuario")
        Dim row1, row2, row3 As DataRow

        Dim valor As DataColumn = New DataColumn("Valor")
        valor.DataType = System.Type.GetType("System.String")

        Dim vineta As DataColumn = New DataColumn("Vineta")
        vineta.DataType = System.Type.GetType("System.String")

        tabla.Columns.Add(valor)
        tabla.Columns.Add(vineta)

        row1 = tabla.NewRow()
        row1.Item("Valor") = "1"
        row1.Item("Vineta") = "USUARIO135"
        tabla.Rows.Add(row1)

        row2 = tabla.NewRow()
        row2.Item("Valor") = "2"
        row2.Item("Vineta") = "JMOREIRA"
        tabla.Rows.Add(row2)

        row3 = tabla.NewRow()
        row3.Item("Valor") = "3"
        row3.Item("Vineta") = "MMERINO"
        tabla.Rows.Add(row3)

        Dim ds As New DataSet
        ds = New DataSet
        ds.Tables.Add(tabla)
        cboUsuarios.DataSource = ds.Tables(0)
        cboUsuarios.DisplayMember = "Vineta"
        cboUsuarios.ValueMember = "Valor"


        'cboUsuarios.Items(0).Text = "USUARIO135"
        'cboUsuarios.Items(0).Value = 1

        'cboUsuarios.Items(1).Text = "JMOREIRA"
        'cboUsuarios.Items(1).Value = 2

        'cboUsuarios.Items(2).Text = "MMERINO"
        'cboUsuarios.Items(2).Value = 3



        'cboUsuarios.Items.Add("USUARIO135ddd")
        ' cboUsuarios.Items.Add("JMOREIRAddd")
        'cboUsuarios.Items.Add("MMERINOddd")



        'foreach ( string val in Enum.GetNames(typeof(Algorithm)) )
        '	{
        '		cboAlgoritmo.Items.Add( val );
        '	}
    End Sub

    Structure Elementos
        Dim idBase As Integer
        Dim TotalItems As Double
    End Structure

    Public Sub Valida(ByVal NomBase As String, ByRef item As AdvAlchemy.Item, ByRef visor As AdvAlchemy.Viewer)
        Dim myDataRow As DataRow
        visor.Visible = True

        Dim tabla As String



        'MessageBox.Show(cboUsuarios.Text)

        ' MessageBox.Show(cboUsuarios.SelectedText)

        'MessageBox.Show(cboUsuarios.SelectedValue)

        If (cboUsuarios.SelectedValue = "1") Then
            tabla = "ALCHEMY_DUPLICADO"
        ElseIf (cboUsuarios.SelectedValue = "2") Then
            tabla = "ALCHEMY_DUPLICADO_JM"
        Else
            tabla = "ALCHEMY_DUPLICADO_MM"
        End If


        If Not (IsNothing(item)) Then
            'If (item.ID >= 143180) Then '((item.ID >= 156380) And (item.ID <= 156425)) Then
            myDataRow = DtsReporteScan.Escaneo.NewEscaneoRow()
            If Not (item.IsFolder Or item.IsRoot Or item.Format = AdvAlchemy.AuFormatEnum.auFmtOCR Or item.Format = AdvAlchemy.AuFormatEnum.auFmtBinary Or item.Format = AdvAlchemy.AuFormatEnum.auFmtUnknown Or item.Format = AdvAlchemy.AuFormatEnum.auFmtUnknown Or item.Format = AdvAlchemy.AuFormatEnum.auFmtASCII) Then
                Dim INICIOCAD As Integer = InStr(item.Path.ToUpper, "\") 'txtEquipos.Text.ToUpper)
                Dim cadena As String = IIf(Len(item.Path) > 0, Mid(item.Path.ToUpper, IIf(INICIOCAD = 0, 1, INICIOCAD)) & "-", "")
                ' MsgBox(Item.Date.Hour)
                Dim horaini As Integer
                Dim horafin As Integer
                horaini = 7
                horafin = 24
                visor.Application.CanWrite = True
                If item.Format = AdvAlchemy.AuFormatEnum.auFmtPDF Then
                    Dim tbHashFile As String = ""
                    Dim tmpFile As String
                    Dim ra As Integer
                    Try
                        tmpFile = strDrive.Trim & strRuta.Trim & "\archihash" '& item.Title
                        item.Retrieve(tmpFile)
                        PdfDocument1.Load(tmpFile)
                        myDataRow("paginas") = PdfDocument1.PageCount
                        PdfDocument1.Close()
                        'SISZU
                        If (Len(item.Path) > 0) Then
                            Dim chk As New CalcularValoreHashCs.Checksum
                            tbHashFile = chk.CalculateFileHash(tmpFile, CType(cboAlgoritmo.SelectedIndex, CalcularValoreHashCs.Algorithm))

                        End If
                        FileSystem.Kill(tmpFile)
                        'visor.ViewItem(item)
                    Catch ex As Exception

                        'MessageBox.Show(CType(item.ID, String) + " " + NomBase + " " + item.Path)
                        Dim sql As String = "insert into ALCHEMY_DUPLICADO_ERROR (IDS,path,NOMBASE,USUARIO) values (" + CType(item.ID, String) + ",'" + ex.Message.ToString + "','" + NomBase + "','" + tabla + "')"
                        Dim cmd As New OracleCommand(sql, conn)
                        cmd.CommandType = CommandType.Text
                        ra = cmd.ExecuteNonQuery()
                    End Try
                    'conn.ConnectionString = oradb
                    'Dim ra As Integer
                    Try
                        'conn.Open()
                        'Dim sql As String = "insert into alchemy_duplicado_3 (IDS,PATH,COD_HASH,NOMBASE) values (" + CType(item.ID, String) + "," + CType(item.Path, String) + "," + CType(tbHashFile, String) + "," + CType(NomBase, String) + ")"
                        Dim sql As String = "insert into " + tabla + " (IDS,COD_HASH,NOMBASE,paginas) values (" + CType(item.ID, String) + ",'" + tbHashFile + "','" + NomBase + "','" + myDataRow("paginas").ToString + "')"
                        Dim cmd As New OracleCommand(sql, conn)
                        cmd.CommandType = CommandType.Text
                        ra = cmd.ExecuteNonQuery()
                    Catch ex As Exception
                        'MessageBox.Show("Insert " + CType(item.ID, String) + " " + NomBase)
                        'MessageBox.Show(ex.Message.ToString)
                        Try
                            Dim sql As String = "insert into ALCHEMY_DUPLICADO_ERROR (IDS,PATH,NOMBASE,usuario) values (" + CType(item.ID, String) + ",'" + ex.Message.ToString + "','" + NomBase + "','" + tabla + "')"
                            Dim cmd As New OracleCommand(sql, conn)
                            cmd.CommandType = CommandType.Text
                            ra = cmd.ExecuteNonQuery()
                        Catch ee As Exception
                        End Try
                        'Finally
                        'conn.Close()
                        'conn.Dispose()
                    End Try
                Else
                    Dim ra As Integer
                    Try
                        visor.ViewItem(item)
                        Dim tbHashFile As String = ""
                        Dim tmpFile As String

                        Try
                            myDataRow("paginas") = IIf(visor.PageCount <= 1, 1, visor.PageCount)
                            'SISZU
                            tmpFile = strDrive.Trim & strRuta.Trim & "\archihashs" '& item.Title
                            item.Retrieve(tmpFile)
                            'PdfDocument1.Load(tmpFile)
                            'myDataRow("paginas") = PdfDocument1.PageCount
                            'PdfDocument1.Close()
                            If (Len(item.Path) > 0) Then
                                Dim chk As New CalcularValoreHashCs.Checksum
                                tbHashFile = chk.CalculateFileHash(tmpFile, CType(cboAlgoritmo.SelectedIndex, CalcularValoreHashCs.Algorithm))
                            End If
                            FileSystem.Kill(tmpFile)
                            'visor.ViewItem(item)
                        Catch ex As Exception

                            'MessageBox.Show(CType(item.ID, String) + " " + NomBase + " " + item.Path)
                            Dim sql As String = "insert into ALCHEMY_DUPLICADO_ERROR (IDS,path,NOMBASE,usuario) values (" + CType(item.ID, String) + ",'" + ex.Message.ToString + "','" + NomBase + "','" + tabla + "')"
                            Dim cmd As New OracleCommand(sql, conn)
                            cmd.CommandType = CommandType.Text
                            ra = cmd.ExecuteNonQuery()
                            'MessageBox.Show(CType(item.ID, String) + " " + NomBase + " " + item.Path)
                        End Try
                        'conn.ConnectionString = oradb

                        Try
                            'conn.Open()
                            'Dim sql As String = "insert into alchemy_duplicado_3 (IDS,CODIGO,COD_HASH) values (" + TextBox1.Text + ",'" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')"
                            'Dim sql As String = "insert into alchemy_duplicado_3 (IDS,PATH,COD_HASH,NOMBASE) values (" + CType(item.ID, String) + "," + CType(item.Path, String) + "," + CType(tbHashFile, String) + "," + CType(NomBase, String) + ")"
                            Dim sql As String = "insert into " + tabla + " (IDS,COD_HASH,NOMBASE,paginas) values (" + CType(item.ID, String) + ",'" + tbHashFile + "','" + NomBase + "'  ,'" + myDataRow("paginas").ToString + "' )"
                            'Dim sql As String = "insert into alchemy_duplicado_3 (IDS) values (" + CType(item.ID, String) + ")"
                            Dim cmd As New OracleCommand(sql, conn)
                            cmd.CommandType = CommandType.Text
                            ra = cmd.ExecuteNonQuery()
                        Catch ex As Exception
                            Try
                                Dim sql As String = "insert into ALCHEMY_DUPLICADO_ERROR (IDS,PATH,NOMBASE,usuario) values (" + CType(item.ID, String) + ",'" + ex.Message.ToString + "','" + NomBase + "','" + tabla + "')"
                                Dim cmd As New OracleCommand(sql, conn)
                                cmd.CommandType = CommandType.Text
                                ra = cmd.ExecuteNonQuery()
                            Catch ee As Exception
                            End Try
                            'MessageBox.Show("Insert " + CType(item.ID, String) + " " + NomBase)
                            'MessageBox.Show(ex.Message.ToString)
                            'Finally
                            'conn.Close()
                            'conn.Dispose()
                        End Try




                    Catch ex As Exception
                        Dim sql As String = "insert into ALCHEMY_DUPLICADO_ERROR (IDS,PATH,NOMBASE,usuario) values (" + CType(item.ID, String) + ",'" + ex.Message.ToString + "','" + NomBase + "','" + tabla + "')"
                        Dim cmd As New OracleCommand(sql, conn)
                        cmd.CommandType = CommandType.Text
                        ra = cmd.ExecuteNonQuery()

                    End Try
                End If
                'End If
                Try
                    myDataRow("id") = item.ID
                    DtsReporteScan.BaseDatos.Rows(ContadorBase).Item(2) = item.ID
                    myDataRow("BaseDatos") = NomBaseDatos.ToUpper
                    myDataRow("titulo") = item.Title
                    myDataRow("fecha") = item.Date.Date
                    myDataRow("equipo") = Mid(cadena, 1, IIf(InStr(cadena, "\") = 0, 1, InStr(cadena, "\") - 1))
                    DtsReporteScan.Escaneo.AddEscaneoRow(myDataRow)
                    'uGrid.Refresh()
                Catch ex As Exception
                End Try
            End If
            'End If  'del if
        End If
        Return
    End Sub

    Sub Iniciar(ByVal NomBases As String, ByVal Bases As String, ByVal Inicial As Int64, ByVal Final As Int64)
        ' MsgBox(Bases)
        '  brayen()
        Dim contador As Double
        TERMOMETRO.Maximum = itm.TotalItems
        Viewer = AuApp.NewViewer(pnl1.Handle.ToInt32)
        Viewer1 = AuApp.NewViewer(pnl2.Handle.ToInt32)
        Viewer2 = AuApp.NewViewer(pnl3.Handle.ToInt32)
        Viewer3 = AuApp.NewViewer(pnl4.Handle.ToInt32)
        Viewer4 = AuApp.NewViewer(pnl5.Handle.ToInt32)
        Viewer5 = AuApp.NewViewer(pnl6.Handle.ToInt32)
        Viewer6 = AuApp.NewViewer(pnl7.Handle.ToInt32)
        Viewer7 = AuApp.NewViewer(pnl8.Handle.ToInt32)
        Viewer8 = AuApp.NewViewer(pnl9.Handle.ToInt32)



        AuDbOri = AuApp.Databases.Add(Bases)

        itm.idBase = 0
        itm.TotalItems = Final


        TERMOMETRO.Minimum = Inicial
        TERMOMETRO.Maximum = itm.TotalItems

        'siszu
        conn.ConnectionString = oradb
        conn.Open()
        Dim cont As Int32 = 0

        For contador = Inicial + 1 To itm.TotalItems Step 9
            cont = cont + 1

            TERMOMETRO.Value = contador
            txtID.Text = "ID: {" & contador.ToString & " / " & TERMOMETRO.Maximum.ToString & "}"
            '  Application.DoEvents()

            'UPGRADE_WARNING: No se puede resolver la propiedad predeterminada del objeto Database.GetItemByID. Haga clic aquí para obtener más información: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'

            Do While flagPause
                Windows.Forms.Application.DoEvents()
            Loop

            Item = AuDbOri.GetItemByID(contador)
            Item2 = AuDbOri.GetItemByID(contador + 1)
            Item3 = AuDbOri.GetItemByID(contador + 2)
            Item4 = AuDbOri.GetItemByID(contador + 3)
            Item5 = AuDbOri.GetItemByID(contador + 4)
            Item6 = AuDbOri.GetItemByID(contador + 5)
            Item7 = AuDbOri.GetItemByID(contador + 6)
            Item8 = AuDbOri.GetItemByID(contador + 7)
            Item9 = AuDbOri.GetItemByID(contador + 8)

            Valida(NomBases, Item, Viewer)
            Valida(NomBases, Item2, Viewer1)
            Valida(NomBases, Item3, Viewer2)
            Valida(NomBases, Item4, Viewer3)
            Valida(NomBases, Item5, Viewer4)
            Valida(NomBases, Item6, Viewer5)
            Valida(NomBases, Item7, Viewer6)
            Valida(NomBases, Item8, Viewer7)
            Valida(NomBases, Item9, Viewer8)
            ''Dim sender As Object
            ''Dim e As System.ComponentModel.DoWorkEventArgs
            ''BackgroundWorker1_DoWork(sender, e)

            'lblitem.Text = "Base Actual : " & NomBaseDatos
            Windows.Forms.Application.DoEvents()
            If (cont = 50) Then
                ClearMemory()
            End If


        Next

        conn.Close()
        conn.Dispose()

        '   uGrid.Refresh()
        Viewer = Nothing
        Viewer1 = Nothing
        Viewer2 = Nothing
        Viewer3 = Nothing
        Viewer4 = Nothing
        Viewer5 = Nothing
        Viewer6 = Nothing
        Viewer7 = Nothing
        Viewer8 = Nothing

        'Application.DoEvents()
    End Sub

    Sub Botones(ByVal Estado As Boolean)
        cmdLoadAlchemy.Enabled = Estado
        cmdINICIAR.Enabled = Estado
        cmdSaveAdvance.Enabled = Not Estado
        cmdPause.Enabled = Not Estado
    End Sub
    'done
    Private Sub cmdINICIAR_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdINICIAR.Click
        lblStatus.Text = "Contabilizando"
        ClearMemory()
        Botones(False)

        Dim cONT1 As Integer
        TermBases.Minimum = 1
        TermBases.Value = 1
        TermBases.Maximum = DtsReporteScan.BaseDatos.Count
        Dim strStreamW As Stream = Nothing
        Dim PathArchivo = strDrive.Trim & strRuta.Trim & "\conteo.txt"
        If Not (File.Exists(PathArchivo)) Then
            strStreamW = File.Create(PathArchivo) ' lo creamos
            DtsReporteScan.Escaneo.Clear()
        End If

        'Aqui comienza el conteo
        ' MsgBox(DtsReporteScan.BaseDatos.Count)
        Temporizador.Start()
        Timer1.Start()
        For ContadorBase = 0 To DtsReporteScan.BaseDatos.Count - 1
            ' MsgBox(DtsReporteScan.BaseDatos.Rows(ContadorBase).Item(0).ToString())
            TermBases.Value = ContadorBase + 1
            NomBaseDatos = DtsReporteScan.BaseDatos.Rows(ContadorBase).Item(0).ToString()
            Iniciar(NomBaseDatos, DtsReporteScan.BaseDatos.Rows(ContadorBase).Item(1).ToString(), Val(DtsReporteScan.BaseDatos.Rows(ContadorBase).Item(2).ToString()), Val(DtsReporteScan.BaseDatos.Rows(ContadorBase).Item(3).ToString()))
        Next



        MessageBox.Show("Proceso Terminado", "Contador", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        Timer1.Stop()
        Temporizador.Stop()
        'Botones(True)

    End Sub

    'DONE
    Private Sub cmdSaveAdvance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveAdvance.Click
        ClearMemory()
        lblStatus.Text = "Pausa"
        Windows.Forms.Application.DoEvents()
        cmdPause_Click(sender, e)
        lblStatus.Text = "Grabando"
        Temporizador_Tick(sender, e)
        lblStatus.Text = "Contabilizando"
        cmdPause_Click(sender, e)
    End Sub

    'DONE
    Private Sub cmdLoadAlchemy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadAlchemy.Click

        AuApp.LoadOptionsFile()
        For Each Database In AuApp.Databases
            Dim myDataRow As DataRow = DtsReporteScan.BaseDatos.NewBaseDatosRow()
            myDataRow("Nombre") = Database.Title.ToUpper
            myDataRow("NombreBase") = Database.Path
            AuDbOri = AuApp.Databases.Add(Database.Path)
            myDataRow("Ultimo") = 0
            myDataRow("Total") = IIf(AuDbOri.LastBuiltID < AuDbOri.LastUnbuiltID, AuDbOri.LastUnbuiltID, AuDbOri.LastBuiltID)

            AuDbOri = Nothing
            DtsReporteScan.BaseDatos.AddBaseDatosRow(myDataRow)
        Next


    End Sub
    'DONE
    Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
        ClearMemory()
        flagPause = Not flagPause
        If flagPause Then
            lblStatus.Text = "Pausado..."
            cmdPause.Text = "Continuar"
        Else
            lblStatus.Text = "Contabilizando"
            cmdPause.Text = "Pausa"
        End If
    End Sub
    'DONE
    Private Sub cmdContabilizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdContabilizar.Click
        Timer1_Tick(sender, e)
    End Sub
    'DONE
    Private Sub cmdMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMax.Click
        TermBases.Minimum = 0
        TermBases.Maximum = DtsReporteScan.BaseDatos.Count
        For ContadorBase = 0 To DtsReporteScan.BaseDatos.Count - 1
            '  MsgBox(DtsReporteScan.BaseDatos.Rows(ContadorBase).Item(0).ToString())

            TermBases.Value = ContadorBase + 1
            NomBaseDatos = DtsReporteScan.BaseDatos.Rows(ContadorBase).Item(0).ToString()

            AuDbOri = AuApp.Databases.Add(DtsReporteScan.BaseDatos.Rows(ContadorBase).Item("NombreBase").ToString)
            DtsReporteScan.BaseDatos.Rows(ContadorBase).Item("Total") = IIf(AuDbOri.LastBuiltID < AuDbOri.LastUnbuiltID, AuDbOri.LastUnbuiltID * 1.25, AuDbOri.LastBuiltID * 1.25)
            AuDbOri = Nothing
        Next

    End Sub
    'DONE
    Private Sub Temporizador_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Temporizador.Tick
        DtsReporteScan.BaseDatos.WriteXml(strDrive.Trim & strRuta.Trim & "\bases.xml")
        DtsReporteScan.Escaneo.WriteXml(strDrive.Trim & strRuta.Trim & "\data.xml")
    End Sub
    'DONE
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ClearMemory()
        lblStatus.Text = "Pausa"
        Windows.Forms.Application.DoEvents()
        cmdPause_Click(sender, e)
        lblStatus.Text = "OBTENIENDO TOTALES"

        Dim q
        q = From p In DtsReporteScan.Escaneo _
                        Group By Base = p("BaseDatos") _
                        Into CountDocumentos = Count(p("id")), SumDocumentos = Sum(CDbl(p("paginas")))
                        Select Base, CountDocumentos, SumDocumentos

        DtsReporteScan.Resultados.Clear()
        For Each p As Object In q
            'MsgBox(p.Base & vbCrLf & "Documentos : " & p.CountDocumentos & "  -  Paginas : " & p.SumDocumentos)
            Dim myDataRow As DataRow = DtsReporteScan.Resultados.NewResultadosRow
            myDataRow("BaseDatos") = p.Base
            myDataRow("CantDoc") = p.CountDocumentos
            myDataRow("NumPag") = p.SumDocumentos
            DtsReporteScan.Resultados.AddResultadosRow(myDataRow)
        Next



        lblStatus.Text = "Contabilizando"
        cmdPause_Click(sender, e)
    End Sub


End Class